package Homework;

//浪费一个空间
public class MyCircularQueue {
    public int[] elem;
    public int front;
    public int rear;

    public MyCircularQueue(int k) {
        elem = new int[k+1];
    }

    public boolean enQueue(int value) {
        if(front == rear ){
            elem[front] = value;
            rear = (rear + 1) % elem.length;
            return true;
        }
        if((rear + 1) % elem.length == front){
            return false;
        }
        elem[rear] = value;
        rear = (rear + 1) % elem.length;
        return true;
    }

    public boolean deQueue() {
        if(front == rear){
            return false;
        }
        front = (front + 1) % elem.length;
        return true;
    }

    public int Front() {
        if(front == rear){
            return -1;
        }
        return elem[front];
    }

    public int Rear() {
        if(front == rear){
            return -1;
        }
        return elem[(rear + elem.length - 1) % elem.length];
    }

    public boolean isEmpty() {
        if(front == rear){
            return true;
        }
        return false;
    }

    public boolean isFull() {
        if((rear + 1) % elem.length == front){
            return true;
        }
        return false;
    }
}


//使用usedSize
class MyCircularQueue2{
    public int usedSize;
    public int front;
    public int rear;
    public int[] elem;

    public MyCircularQueue2(int k) {
        elem = new int[k];
    }

    public boolean enQueue(int value) {
        if(usedSize == elem.length){
            return false;
        }
       if(usedSize == 0){
           elem[front] = value;
           rear = (rear + 1) % elem.length;
           usedSize++;
           return true;
       }
       elem[rear] = value;
       rear = (rear + 1) % elem.length;
       usedSize++;
       return true;
    }

    public boolean deQueue() {
        if(usedSize == 0){
            return false;
        }
        front = (front + 1) % elem.length;
        usedSize--;
        return true;
    }

    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return elem[front];
    }

    public int Rear() {
        if(isEmpty()){
            return -1;
        }
        return elem[(rear + elem.length -1) % elem.length];
    }

    public boolean isEmpty() {
        if(usedSize == 0){
            return true;
        }
        return false;
    }

    public boolean isFull() {
        if(usedSize == elem.length){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        MyCircularQueue2 myCircularQueue2 = new MyCircularQueue2(3);
        for(int i = 1;i<=4;i++){
            System.out.println(myCircularQueue2.enQueue(i));
        }
    }
}

//使用flag来判断
class MyCircularQueue3{
    public boolean flag;//false代表列表为空
    public int front;
    public int rear;
    public int[] elem;

    public MyCircularQueue3(int k) {
        elem = new int[k];
    }

    public boolean enQueue(int value) {
        if(rear == front){
            if(flag == false){
                elem[front] = value;
                rear = (rear + 1) % elem.length;
                flag = true;
                return true;
            }
            return false;
        }
        elem[rear] = value;
        rear = (rear + 1) % elem.length;
        flag = true;
        return true;
    }

    public boolean deQueue() {
        if(isEmpty()){
            return false;
        }
        front = (front + 1) % elem.length;
        flag = false;
        return true;
    }

    public int Front() {
        if(isEmpty()){
            return -1;
        }
        return elem[front];
    }

    public int Rear() {
        if (isEmpty()){
            return -1;
        }
        return elem[(rear + elem.length - 1) % elem.length];
    }

    public boolean isEmpty() {
        if(flag == false && front == rear){
            return true;
        }
        return false;
    }

    public boolean isFull() {
        if(flag == true && front == rear){
            return true;
        }
        return false;
    }

}

